﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using yncTechWeb.Controls;
using System.Data;
using EntitySpaces.Interfaces;
using System.Web.UI.HtmlControls;

public partial class Admin_Sys_Sec_Sec003 : AdminBasePage
{
    TSRoleFunctionsCollection roleFunctions = new TSRoleFunctionsCollection();
    protected void Page_Load(object sender, EventArgs e)
    {
        roleFunctions.Query.Where(roleFunctions.Query.RoleID == drpRoles.SelectedValue);
        roleFunctions.Query.Load();
    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void drpRoles_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        TSSystemCollection systems = new TSSystemCollection();
        systems.Query.Where(systems.Query.IsActive == (int)ActiveStatus.Active);
        systems.Query.Load();

        pageGrid.DataSource = systems;
        pageGrid.DataBind();
    }

    //绑定系统
    protected void system_ItemBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType.Equals(ListItemType.AlternatingItem)
           || e.Item.ItemType.Equals(ListItemType.Item))
        {
            PagnationGrid grid = e.Item.FindControl("gridChannels") as PagnationGrid;

            var system = e.Item.DataItem as TSSystem;
            
            TSFunctionsCollection functions = new TSFunctionsCollection();
            functions.Query.Where(functions.Query.SystemId == system.SystemId.Value 
                && functions.Query.ParentID <0
                && functions.Query.IsActive == true);
            functions.Query.Load();
            grid.DataSource = functions;
            grid.DataBind();
        }
    }

    //绑定频道
    protected void channel_ItemBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType.Equals(ListItemType.Item) || e.Item.ItemType.Equals(ListItemType.AlternatingItem))
        {
            Repeater grid = e.Item.FindControl("gridFunctions") as Repeater;

            var function = e.Item.DataItem as esTSFunctions;

            TSFunctionsCollection functions = new TSFunctionsCollection();
            functions.Query.Where(functions.Query.ParentID == function.FunctionID.Value 
                && functions.Query.IsActive == true
                && functions.Query.ParentID>=0);
            functions.Query.Load();

            grid.DataSource = functions;
            grid.DataBind();
        }
    }
    //绑定功能
    protected void functions_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType.Equals(ListItemType.Item) || e.Item.ItemType.Equals(ListItemType.AlternatingItem))
        {
            CheckBox chk = e.Item.FindControl("chkFunction") as CheckBox;

            TSFunctions function = e.Item.DataItem as TSFunctions;

            chk.Checked = roleFunctions.Where(rf => rf.FunctionID == function.FunctionID).Count() > 0;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            using (esTransactionScope tr = new esTransactionScope())
            {
                //先删除该用户组所有设定权限
                string roleID = drpRoles.SelectedValue.Trim();

                TSRole role = new TSRole();

                if ( role.LoadByPrimaryKey(decimal.Parse(roleID)))
                {
                    var programs = role.TSRoleFunctionsCollectionByRoleID;
                    programs.MarkAllAsDeleted();
                    programs.Save();
                }

                //判断是否有Function被选择,如果有则添加频道选择项
                bool hasItemSelected = false;

                TSRoleFunctionsCollection functions = new TSRoleFunctionsCollection();
                //逐个添加新权限
                for (int i = 0; i < pageGrid.Items.Count; i++)
                {
                    Repeater rptChannels = (Repeater)pageGrid.Items[i].FindControl("gridChannels");
                    for (int j = 0; j < rptChannels.Items.Count; j++)
                    {
                        hasItemSelected = false;
                        Repeater rptPrograms = (Repeater)rptChannels.Items[j].FindControl("gridFunctions");
                        for (int k = 0; k < rptPrograms.Items.Count; k++)
                        {
                            CheckBox cbSelect = (CheckBox)rptPrograms.Items[k].FindControl("chkFunction");
                            if (cbSelect.Checked)
                            {
                                hasItemSelected = true;
                                HtmlInputHidden txtId = (HtmlInputHidden)rptPrograms.Items[k].FindControl("txtId");

                                TSRoleFunctions rf = new TSRoleFunctions();
                                rf.RoleID = decimal.Parse(drpRoles.SelectedValue);
                                rf.FunctionID = decimal.Parse(txtId.Value.Trim());
                                functions.Add(rf);
                            }
                        }

                        if (hasItemSelected)
                        {
                            HtmlInputHidden txtId = (HtmlInputHidden)rptChannels.Items[j].FindControl("txtId");
                            TSRoleFunctions rf = new TSRoleFunctions();
                            rf.RoleID = decimal.Parse(drpRoles.SelectedValue);
                            rf.FunctionID = decimal.Parse(txtId.Value.Trim());
                            functions.Add(rf);
                        }
                    }

                    functions.Save();
                }
                tr.Complete();
            }

            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Text = "操作成功!";
        }
        catch (Exception ex)
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = ex.Message;
        }
    }
}